﻿@page "/minesweeper"

<link href="css/minesweeper.css" rel="stylesheet" />

<div class="minesweeper-game minesweeper-medium">
    <div class="minesweeper-board" @oncontextmenu:preventDefault onmousedown="@(board.Status != GameStatus.Completed ? "faceOooh(event);" : "")" onmouseup="faceSmile();">
        @{
            var maxWidth = board.Width + 1;
            var maxHeight = board.Height + 1;
        }
        <div class="minesweeper-border-topleft"></div>
        @for (int i = 1; i < maxWidth; i++)
        {
            <div class="minesweeper-border-horizontal"></div>
        }
        <div class="minesweeper-border-topright"></div>
        <div class="minesweeper-border-vertical-long"></div>
        <div class="minesweeper-time-@GetPlace(board.MinesRemaining, 100)" id="mines_hundreds"></div>
        <div class="minesweeper-time-@GetPlace(board.MinesRemaining, 10)" id="mines_tens"></div>
        <div class="minesweeper-time-@GetPlace(board.MinesRemaining, 1)" id="mines_ones"></div>
        @if (board.Status == GameStatus.Failed)
        {
            <div class="minesweeper-face-dead" id="face" style="margin-left:70px; margin-right:70px;" @onclick="@(() => board.Reset())"></div>
        }
        else if (board.Status == GameStatus.Completed)
        {
            <div class="minesweeper-face-win" id="face" style="margin-left:70px; margin-right:70px;" @onclick="@(() => board.Reset())"></div>
        }
        else
        {
            <div class="minesweeper-face-smile" id="face" style="margin-left:70px; margin-right:70px;" @onclick="@(() => board.Reset())"></div>
        }
        <div class="minesweeper-time-@GetPlace(board.Stopwatch.Elapsed.Seconds,100)" id="seconds_hundreds"></div>
        <div class="minesweeper-time-@GetPlace(board.Stopwatch.Elapsed.Seconds,10)" id="seconds_tens"></div>
        <div class="minesweeper-time-@GetPlace(board.Stopwatch.Elapsed.Seconds,1)" id="seconds_ones"></div>
        <div class="minesweeper-border-vertical-long"></div>

        @for (int i = 0; i <= maxWidth; i++)
        {
            @for (int j = 0; j <= maxHeight; j++)
            {
                int x = i;
                int y = j;

                if (x == 0 && y == 0)
                {
                    <div class="minesweeper-border-jointleft"></div>
                }
                else if (x == 0 && y == maxHeight)
                {
                    <div class="minesweeper-border-jointright"></div>
                }
                else if (x == maxWidth && y == 0)
                {
                    <div class="minesweeper-border-bottomleft"></div>
                }
                else if (x == maxWidth && y == maxHeight)
                {
                    <div class="minesweeper-border-bottomright"></div>
                }
                else if (y == 0 || y == maxHeight)
                {
                    <div class="minesweeper-border-vertical"></div>
                }
                else if (x == 0 || x == maxWidth)
                {
                    <div class="minesweeper-border-horizontal"></div>
                }
                else if (y > 0 && y < maxHeight)
                {
                    var currentPanel = board.Panels.First(m => m.X == x && m.Y == y);
                    if (currentPanel.IsRevealed)
                    {
                        if (currentPanel.IsMine) //Mine
                        {
                            <div class="minesweeper-gamepiece minesweeper-mine"></div>
                        }
                        else if (currentPanel.AdjacentMines == 0) //Blank
                        {
                            <div class="minesweeper-gamepiece minesweeper-0"></div>
                        }
                        else //Number
                        {
                            <div class="minesweeper-gamepiece minesweeper-@currentPanel.AdjacentMines">@currentPanel.AdjacentMines</div>
                        }
                    }
                    else if (currentPanel.IsFlagged)
                    {
                        <div class="minesweeper-gamepiece minesweeper-flagged" @oncontextmenu="@(() => board.FlagPanel(x, y))" @oncontextmenu:preventDefault></div>
                    }
                    else //Unrevealed
                    {
                        <div class="minesweeper-gamepiece minesweeper-unrevealed" @onclick="@(() => board.MakeMove(x, y))" @oncontextmenu:preventDefault @oncontextmenu="@(() => board.FlagPanel(x, y))">
                        </div>
                    }
                }
            }
        }
    </div>
</div>

@code {
    GameBoard board = new GameBoard();

    public int GetPlace(int value, int place)
    {
        if (value == 0) return 0;
        return ((value % (place * 10)) - (value % place)) / place;
    }

    protected override async Task OnInitializedAsync()
    {
        await Common.SetTitleAsync("扫雷");
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        while (board.Status == GameStatus.InProgress && Common.Uri().Contains("minesweeper"))
        {
            await Task.Delay(500);
            var elapsedTime = (int)board.Stopwatch.Elapsed.TotalSeconds;
            var hundreds = GetPlace(elapsedTime, 100);
            var tens = GetPlace(elapsedTime, 10);
            var ones = GetPlace(elapsedTime, 1);

            await Common.InvokeAsync("window.func.setMineSweeperTime", hundreds, tens, ones);
        }
    }
}